package com.yxx.sys.controller;


import com.yxx.sys.common.DataGridView;
import com.yxx.sys.common.TreeNode;
import com.yxx.sys.common.TreeNodeBuilder;
import com.yxx.sys.domain.Menu;
import com.yxx.sys.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yxx
 * @since 2022-05-14
 */
@RestController
@RequestMapping("/menu")
@Api("菜单控制器")
public class MenuController {

  @Autowired
  MenuService menuService;

  @GetMapping(value = "/loadIndexLeftMenu")
  @ApiOperation("获取菜单")
  public DataGridView loadIndexLeftMenu(){
    List<Menu> list = menuService.list();
    List<TreeNode> treeNodes = new ArrayList<>();
    for(Menu p:list){
      TreeNode e = new TreeNode();
      e.setId(p.getId());
      e.setPid(p.getPid());
      e.setIcon(p.getIcon());
      e.setHref(p.getHref());
      e.setTitle(p.getTitle());
      treeNodes.add(e);
    }
    //构建层级关系
    List<TreeNode> list2 = TreeNodeBuilder.build(treeNodes, 1);
    return new DataGridView(list2);
  }
}
